<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>07.箭头函数的使用</title>
</head>

<body>
  <script>
    /* 
箭头函数的使用：ES6允许箭头函数的使用
*/
    /* let add = (a, b) => {
      return a + b;
    }
    let result = add(1, 2);
    console.log(result); */

    //1.this是静态的，this始终指向函数声明时所在作用域的this的值
    function getName() {
      console.log(this.name);
    }
    let getName2 = () => {
      console.log(this.name);
    }
    //设置window的name属性
    window.name = "尚硅谷";
    const school = {
      name: "atguigu",
    }
    //1.1直接调用
    // getName();//尚硅谷
    // getName2();//尚硅谷

    //12 call方法调用
    getName.call(school); //atguigu
    getName2.call(school); //尚硅谷

    //2.不能作为构造函数实例化对象
    let person = (name, age) => {
      this.name = name;
      this.age = age;
    }
    //let me = new person("xiao", 30);
    //console.log(me); //07.ES6中箭头函数.html:46 Uncaught TypeError: person is not a constructor

    //3.不能使用arguments变量
    let fn = () => {
      //console.log(arguments); //07.ES6中箭头函数.html:51 Uncaught ReferenceError: arguments is not defined
    }
    fn(1, 2, 3);

    //4.箭头函数的简写
    //4.1 省略小括号,当形参有且只有一个时
    let add = a => {
      return a + a;
    }
    let sum = add(2);
    console.log(sum);

    //4.2 省略大括号(并且可以省略return关键字),当代码体只有一条语句的时候
    let pow = a => a * a;
    let mi = pow(3);
    console.log(mi);
  </script>
</body>

</html>